<?php

declare(strict_types=1);

/**
 * @Author:     Frank
 * @contact     frank_hhm@163.com
 * @Date:       2022-12-07 18:44:14
 * @Last Modified time: 2022-12-07 19:20:15
 */
namespace app\common\services\store;

use app\common\dao\store\GoodsCateDao;

use frank\base\BaseService;
use frank\exception\CommonException;
use frank\utils\helper\ArrayHelper;

/**
 * Class GoodsCateService
 */
class GoodsCateService extends BaseService
{

    /**
     * GoodsCateService constructor.
     * @param GoodsCateDao $dao
     */
    public function __construct(GoodsCateDao $dao)
    {
        $this->dao = $dao;
    }

    public function getGoodsCateDetail($filter)
    {
        $detail = $this->dao->detail($filter);
        if (!$detail) {
            throw new CommonException('数据不存在');
        }
        return $detail->toArray();
    }

    /**
     * 获取分类列表
     */
    public function getGoodsCateList(array $where)
    {
        [$page, $limit] = $this->getPageValue();
        $list = $this->dao->getGoodsCateList($where, $page, $limit);
        $count = $this->dao->count($where);
        $childrenValue = [];
        $list = ArrayHelper::getArrayTreeChildren($list);
        return compact('list', 'count');
    }

    /**
     * 获取分类选择下拉树
     */
    public function getSelectTree($pid = 0)
    {
        $menuList = $this->dao->getGoodsCateAll([], ['id', 'pid', 'cate_name']);
        $list = ArrayHelper::sortListTier($menuList, 0, 'pid', 'id');
        $menus = [['value' => 0, 'label' => '顶级分类']];
        foreach ($list as $menu) {
            $arr = ['value' => $menu['id'], 'label' => $menu['html'] . $menu['cate_name']];
            if($pid && $pid == $menu['id']){
                $arr['disabled'] = true;
            }
            $menus[] = $arr;
        }
        return $menus;
    }

}